//
//  WWRightTableViewCell.m
//  Shopping
//
//  Created by 张德华 on 8/20/16.
//  Copyright © 2016 张湾湾. All rights reserved.
//

#import "WWRightTableViewCell.h"
#import "WWRightCellModel.h"
#import "UIImageView+WebCache.h"
#import "PurchaseCarAnimationTool.h"
@interface WWRightTableViewCell()
/**商品图片*/
@property (nonatomic,strong) UIImageView *iv_iconImageView;
/**名称*/
@property (nonatomic,strong) UILabel *lb_nameLabel;
/**产品规格*/
@property (nonatomic,strong) UILabel *lb_specificsLabel;
/**现价*/
@property (nonatomic,strong) UILabel *lb_priceLabel;
/**原价*/
@property (nonatomic,strong) UILabel *lb_market_price;
/**精选*/
@property (nonatomic,strong) UIButton *btn_jxButton;
/**买一赠一*/
@property (nonatomic,strong) UIImageView *iv_buyOneImageView;
/**添加购物车加号按钮*/
@property (nonatomic,strong) UIButton *btn_increaseButton;
/**购物车减少按钮*/
@property (nonatomic,strong) UIButton *btn_reduceButton;
/**购物车商品数量*/
@property (nonatomic,strong) UILabel *lb_goodsLabel;
/**作为分隔线*/
@property (nonatomic,strong) UIView *v_separaterView;

/**商品数量*/
@property (nonatomic,assign) NSInteger goodsCount;




@end


@implementation WWRightTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self setupUI];
    }
    return self;
}


#pragma mark
#pragma mark - ***************初始化界面
- (void)setupUI {
    
    //------ 添加控件
    [self.contentView addSubview:self.iv_iconImageView];
    [self.contentView addSubview:self.btn_jxButton];
    [self.contentView addSubview:self.lb_nameLabel];
    [self.contentView addSubview:self.iv_buyOneImageView];
    [self.contentView addSubview:self.lb_specificsLabel];
    [self.contentView addSubview:self.lb_priceLabel];
    [self.contentView addSubview:self.lb_market_price];
    [self.contentView addSubview:self.btn_increaseButton];
    [self.contentView addSubview:self.lb_goodsLabel];
    [self.contentView addSubview:self.btn_reduceButton];
    [self.contentView addSubview:self.v_separaterView];
    
    //------ 控件添加约束
    [self.iv_iconImageView makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(CGSizeMake(75, 75));
        make.centerY.equalTo(self);
        make.leading.equalTo(5);
    }];
    
    [self.btn_jxButton makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.iv_iconImageView.top);
        make.leading.equalTo(self.iv_iconImageView.trailing).offset(5);
        make.size.equalTo(CGSizeMake(30, 16));
    }];
    
    [self.lb_nameLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.btn_jxButton.top);
        make.bottom.equalTo(self.btn_jxButton);
        make.leading.equalTo(self.btn_jxButton.trailing).offset(5);
        make.trailing.equalTo(-5);
    }];

    [self.iv_buyOneImageView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.btn_jxButton.bottom).offset(3);
        make.leading.equalTo(self.iv_iconImageView.trailing).offset(5);
        make.size.equalTo(CGSizeMake(40, 16));
    }];
    
    [self.lb_specificsLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.iv_buyOneImageView.bottom).offset(5);
        make.leading.equalTo(self.iv_iconImageView.trailing).offset(5);
    }];
    
    [self.lb_priceLabel makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.iv_iconImageView.trailing).offset(5);
        make.bottom.equalTo(self.iv_iconImageView.bottom);
    }];
    
    [self.lb_market_price makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.lb_priceLabel.trailing).offset(5);
        make.bottom.equalTo(self.lb_priceLabel.bottom);
    }];
    
    [self.btn_increaseButton makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(-5);
        make.bottom.equalTo(-5);
    }];
    
    [self.lb_goodsLabel makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.btn_increaseButton);
        make.trailing.equalTo(self.btn_increaseButton.leading).offset(-5);
    }];
    
    [self.btn_reduceButton makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.btn_increaseButton);
        make.trailing.equalTo(self.lb_goodsLabel.leading).offset(-5);
    }];
    
    [self.v_separaterView makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(5);
        make.trailing.bottom.equalTo(0);
        make.height.equalTo(0.5);
    }];
    
    
    
        self.lb_goodsLabel.hidden = YES;
        self.btn_reduceButton.hidden = YES;
    
    
    self.goodsCount = 0;
}

#pragma mark
#pragma mark - ***************设置数据
- (void)setModel:(WWRightCellModel *)model {
    _model = model;
    
    [self.iv_iconImageView sd_setImageWithURL:[NSURL URLWithString:model.img]];
    self.lb_nameLabel.text = model.name;
    self.lb_specificsLabel.text = model.specifics;
    self.lb_priceLabel.text = [NSString stringWithFormat:@"$%@",model.price];

    //------ 买一赠送判断, 有->显示, 无->不显示
    if ([model.pm_desc isEqualToString:@""]) {
        self.iv_buyOneImageView.hidden = YES;
    } else {
        self.iv_buyOneImageView.hidden = NO;
    }
    
    
    //------ 原价格添加删除线
    NSString *market_priceStr = [NSString stringWithFormat:@"$%@",model.market_price];
    NSInteger length = [market_priceStr length];
    NSDictionary *dict = @{NSStrikethroughStyleAttributeName: @(NSUnderlineStyleSingle | NSUnderlinePatternSolid),NSStrikethroughColorAttributeName: [UIColor darkGrayColor]};
    NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:market_priceStr attributes:dict];
    [attri addAttributes:dict range:NSMakeRange(0, length-1)];
    [self.lb_market_price setAttributedText:attri];
    
}

#pragma mark
#pragma mark - ***************添加购物车按钮事件监听
- (void)increaseBtnDidClick {
    if ([self.delegate respondsToSelector:@selector(increaseShoppingCar:)]) {
        [self.delegate increaseShoppingCar:self];
    }
    
    self.lb_goodsLabel.text = [NSString stringWithFormat:@"%ld",++_goodsCount];
    
    if (self.goodsCount > 0) {
        
        self.lb_goodsLabel.hidden = NO;
        self.btn_reduceButton.hidden = NO;
        
        // 测试
        [[WWShopCarManager sharedShopCarManager] addSupermarketGoodsToShopCar:self.model];
    }
    // 将图片的rect 转为相对于屏幕的
    CGRect rect = [self.iv_iconImageView convertRect:self.iv_iconImageView.bounds toView:nil];
    
    [[PurchaseCarAnimationTool shareTool]startAnimationandView:self.iv_iconImageView andRect:rect andFinisnRect:CGPointMake(ScreenWidth/4*2.5, ScreenHeight-49) andFinishBlock:^(BOOL finisn){
        // 发送通知 改变 tabBar上的内容
        //        UIView *tabbarBtn = self.tabBarController.tabBar.subviews[3];
        //        [PurchaseCarAnimationTool shakeAnimation:tabbarBtn];
        NSLog(@"发送通知 改变 tabBar上的内容");
    }];

}

- (void)reduceBtnDidClick {
    if ([self.delegate respondsToSelector:@selector(reduceShoppingCar:)]) {
        [self.delegate reduceShoppingCar:self];
    }
    
    self.lb_goodsLabel.text = [NSString stringWithFormat:@"%ld",--_goodsCount];
    
    if (self.goodsCount <= 0) {
        self.goodsCount = 0;
        self.lb_goodsLabel.hidden = YES;
        self.btn_reduceButton.hidden = YES;
    }
    
    
    
}



#pragma mark
#pragma mark - ***************懒加载
- (UIButton *)btn_jxButton {
    if (!_btn_jxButton) {
        _btn_jxButton = [[UIButton alloc] init];
        [_btn_jxButton setBackgroundImage:[UIImage imageNamed:@"btn.png"] forState:UIControlStateNormal];
        [_btn_jxButton setTitle:@"精选" forState:UIControlStateNormal];
        [_btn_jxButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [_btn_jxButton.titleLabel setFont:[UIFont systemFontOfSize:10]];
        [_btn_jxButton sizeToFit];
    }
    return _btn_jxButton;
}

- (UIImageView *)iv_iconImageView {
    if(!_iv_iconImageView){
        _iv_iconImageView = [[UIImageView alloc] init];
        _iv_iconImageView.image = [UIImage imageNamed:@"v2_placeholder_square"];
        [_iv_iconImageView sizeToFit];
    }
    return _iv_iconImageView;
}

- (UILabel *)lb_nameLabel {
    if(!_lb_nameLabel){
        _lb_nameLabel = [[UILabel alloc] init];
        _lb_nameLabel.font = [UIFont systemFontOfSize:15];
        [_lb_nameLabel sizeToFit];
    }
    return _lb_nameLabel;
}

- (UIImageView *)iv_buyOneImageView {
    if(!_iv_buyOneImageView) {
        _iv_buyOneImageView = [[UIImageView alloc] init];
        _iv_buyOneImageView.image = [UIImage imageNamed:@"buyOne.png"];
        [_iv_buyOneImageView sizeToFit];
    }
    return _iv_buyOneImageView;
}

- (UILabel *)lb_specificsLabel {
    if(!_lb_specificsLabel){
        _lb_specificsLabel = [[UILabel alloc] init];
        _lb_specificsLabel.font = [UIFont systemFontOfSize:11];
        _lb_specificsLabel.textColor = [UIColor grayColor];
        [_lb_specificsLabel sizeToFit];
    }
    return _lb_specificsLabel;
}

- (UILabel *)lb_priceLabel {
    if(!_lb_priceLabel){
        _lb_priceLabel = [[UILabel alloc] init];
        _lb_priceLabel.font = [UIFont systemFontOfSize:14];
        _lb_priceLabel.textColor = [UIColor redColor];
        [_lb_priceLabel sizeToFit];
    }
    return _lb_priceLabel;
}

- (UILabel *)lb_market_price {
    if(!_lb_market_price){
        _lb_market_price = [[UILabel alloc] init];
        _lb_market_price.font = [UIFont systemFontOfSize:12];
        _lb_market_price.textColor = [UIColor darkGrayColor];
        [_lb_market_price sizeToFit];
    }
    return _lb_market_price;
}

- (UIButton *)btn_increaseButton {
    if (!_btn_increaseButton) {
        _btn_increaseButton = [[UIButton alloc] init];
        [_btn_increaseButton setBackgroundImage:[UIImage imageNamed:@"v2_increase"] forState:UIControlStateNormal];
        [_btn_increaseButton addTarget:self action:@selector(increaseBtnDidClick) forControlEvents:UIControlEventTouchUpInside];
        [_btn_increaseButton sizeToFit];
    }
    return _btn_increaseButton;
}

- (UIButton *)btn_reduceButton {
    if (!_btn_reduceButton) {
        _btn_reduceButton = [[UIButton alloc] init];
        [_btn_reduceButton setBackgroundImage:[UIImage imageNamed:@"v2_reduce"] forState:UIControlStateNormal];
        [_btn_reduceButton addTarget:self action:@selector(reduceBtnDidClick) forControlEvents:UIControlEventTouchUpInside];
        [_btn_reduceButton sizeToFit];
    }
    return _btn_reduceButton;
}

- (UILabel *)lb_goodsLabel {
    if (!_lb_goodsLabel) {
        _lb_goodsLabel = [[UILabel alloc] init];
        _lb_goodsLabel.font = [UIFont systemFontOfSize:12];
        
        [_lb_goodsLabel sizeToFit];
    }
    return _lb_goodsLabel;
}

- (UIView *)v_separaterView {
    if (!_v_separaterView) {
        _v_separaterView = [[UIView alloc] init];
        _v_separaterView.backgroundColor = [UIColor colorWithRed:(218)/256.0 green:(218)/256.0 blue:(218)/256.0 alpha:1.0];
    }
    return _v_separaterView;
}









@end
